package com.speakglobe.admin.dao;

import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.speakglobe.admin.model.SystemProperty;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class SystemPropertyDaoImpl implements SystemPropertyDao {

	private HibernateTemplate hibernateTemplate;
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public SystemProperty save(SystemProperty systemProperty) {
		return (SystemProperty)hibernateTemplate.merge(systemProperty);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void delete(SystemProperty systemProperty) {
		hibernateTemplate.delete(systemProperty);
	}

	@Override
	public SystemProperty get(int systemPropertyId) {
		return (SystemProperty)hibernateTemplate.get(SystemProperty.class,systemPropertyId);
	}

	@Override
	public SystemProperty findByKey(String key) {
		SystemProperty systemProperty = null;
		List<SystemProperty> systemProperties = (List<SystemProperty>)hibernateTemplate.find("from SystemProperty where key=?",key);
		
		if(systemProperties != null && systemProperties.size() > 0) {
			if(systemProperties.size() == 1) {
				systemProperty = systemProperties.get(0);
			} else {
				throw new javax.persistence.NonUniqueResultException("Found more than one SystemProperty for key:"+key);
			}
		}
		
		return systemProperty;
	}

	@Override
	public List<SystemProperty> getAllSystemProperties() {
		return hibernateTemplate.find("from SystemProperty");
	}
}
